package org.example.rental.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.rental.common.Result;
import org.example.rental.domain.ReturnCar;
import org.example.rental.service.ReturnCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/returnCar")
public class ReturnCarController extends BaseController {


    @Autowired
    ReturnCarService returnCarService;


    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id){
        ReturnCar returnCar = returnCarService.getById(id);
        return Result.succ(returnCar);
    }


    //生成还车单
    @PostMapping("/add")
    public Result add(@RequestBody ReturnCar returnCar){
        returnCarService.add(returnCar);
        return Result.succ(returnCar);
    }

    @GetMapping("/list")
    public Result list(String orderNum,String carNum,String status){
        QueryWrapper<ReturnCar> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(orderNum),"order_num",orderNum);
        wrapper.eq(StrUtil.isNotBlank(carNum),"car_num",carNum);
        wrapper.eq(StrUtil.isNotBlank(status),"status",status);
        Page<ReturnCar> page = returnCarService.page(getPage(), wrapper);
        return Result.succ(page);
    }



    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Long[] ids){
        //删除还车单
        returnCarService.removeByIds(Arrays.asList(ids));


        return Result.succ("还车单删除成功");

    }


    @PostMapping("/process")
    public Result process(@RequestBody ReturnCar returnCar){

        returnCarService.process(returnCar);

        return Result.succ(returnCar);
    }

    @PostMapping("/comput")
    public Result comput(@RequestBody ReturnCar returnCar){
        Double money = returnCarService.comput(returnCar);
        return Result.succ(money);
    }


}
